<?php

defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'detail';
if($_RQ['op']=='detail'){
	/*******main*******/
	$return['ischecklogin']=$this->model('member')->checklogin();
	if(intval($_RQ['fromuser'])){
		$this->model('member')->setfromuser();
	}
	$id = intval($_RQ['id']);
	if(!$id){
		exi('该商品不存在!','error');
	}
	$this->session('goodsid',$id);
	$team=pdo_get('jy_robshop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($team && $team['status']==0){
		$this->model('model')->add_yao($team);
	}
	if($team['is_vipbuy'] && $team['vipprice']>0){
		$team['isvip']=$this->model('team')->check_vipbuy(0,$team);
	}
	$team['fee']=$team['isvip']?$team['vipprice']:$team['price'];
	$day=date('Y/m/d');
	$hourkey=$this->model('model')->gethourkey();
	$team['isnum']=pdo_count('jy_robshop_canlog',array('goodsid'=>$team['id'],'uniacid'=>$_SESSION['uniacid']),'id');
	if(!$team['goodstype']){
		$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$team['goodsid']));
		$goods['param']=json_decode($goods['param'],true);
		$goods['images']=json_decode($goods['images'],true);
		$team['image']=$goods['image'];
		$team['oldprice']=$goods['price'];
	}else{
		$team['oldprice']=$team['price']*2;
	}
	
	$team['salenum']=pdo_count('jy_robshop_canlog',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$team['id']),'id');
	$team['gongredfee']=pdo_getsum('jy_robshop_gongfeelog',array('goodsid'=>$team['id'],'uniacid'=>$_SESSION['uniacid']),'fee');
	$team['statustitle']=$this->goods_status[$team['status']];
	if($team['status']==1){
				//全天
				if($team['jian_openday']){
					$rest_time=$team['createtime']+$team['jian_days']*24*3600;
					if($rest_time<time()){
						$rest_time=strtotime($day.' 24:00:00');
					}
					$team['hourtype']=1;
				}else{
					$team['hours']=json_decode($team['jian_hour'.$hourkey],true);
					$start_time=strtotime($day.' '.$team['hours']['start']);
					$end_time=strtotime($day.' '.$team['hours']['end']);
					if(TIMESTAMP>$end_time){
						if($hourkey!=2){
							$hourkey ++;
							$team['nexthours']=json_decode($team['jian_hour'.$hourkey],true);
							$start_time=strtotime($day.' '.$team['nexthours']['start']);
							$team['hourtype']=0;
							$rest_time=$start_time;
						}else{
							$team['hourtype']=2;
							$rest_time='';
						}
					}elseif(TIMESTAMP>$start_time){
						$team['hourtype']=1;
						$rest_time=$end_time;
					}else{
						$team['hourtype']=0;
						$rest_time=$start_time;
					}
				}
				$team['hourkey']=$this->hourkey[$hourkey];
				if($rest_time){
					$team['rest_time']=model('jy_weishop/model')->date($rest_time-TIMESTAMP);
				}
	}
	$team['createtime']=date('Y/m/d H:i:s',$team['createtime']);
	$return['team']=$team;
	$return['goods']=$goods;
	$return['list']=array();
	if(!$goods['type']){
		$sendtype=1;
	}else{
		$sendtype=3;
	}
	$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'','isdefault desc');
	$address['pcd']=json_decode($address['pcd'],true);
	$return['post']=array('goodsid'=>$team['id'],'sendtype'=>$sendtype,'address'=>$address);
	$return['params']=array('status'=>0,'goodsid'=>$team['id']);
	$return['pluginset']['jian_feetype']=$return['pluginset']['jian_feetype']?:3;
	$return['pluginset']['jian_feetypetitle']=$this->credit_type[$return['pluginset']['jian_feetype']];
	$return['sharedata']=array(
			'title'=>$nickname.'-发起抢购活动啦！参与就可获利了！',
			'desc'=>'参与抢购活动啦，参与就可获利了！',
			'link'=>custom_url('jy_robshop/detail//id='.$team['id'].'&fromuser='.$_SESSION['uid'],true),
			'imgUrl'=>tomedia($team['image'])
	);
	if($this->setting['tpl_type']){
		//$return['htmls']=$this->template_url('detail1');
	}
	$return['btitle']=$this->btitle;
	set_html('title','抢购详情');
	exi($return);	
}
if($_RQ['op']=='getlist'){
	$team=pdo_get('jy_robshop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$this->session('goodsid')));
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$this->session('goodsid'));
	if($_RQ['status']==3){
		$params['status']=array(3,4,6,7);
	}elseif($_RQ['status']==1){
		$params['status']=array(2,5);
	}else{
		$params['status']=array(0,1);
	}
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_robshop_canlog',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	foreach($return['list'] as &$v){
		$v['statustitle']=$this->canlog_status[$v['status']];
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
		$v['daytitle']=$v['dayid'].' '.$this->hourkey[$v['hourkey']];
		$v['member']=pdo_get('core_members',array('id'=>$v['mid']),array('nickname','avatar'));
		$v['member']['nickname']=$v['member']['nickname']?cfc('tools')->substr($v['member']['nickname'],0,1).'****':'******';
	}
	exi($return);
}
// 完成下单
if($_RQ['op']=='post'){
	$post=get_postdata();
	$goodsid=$this->session('goodsid');
	if($_RQ['isyue']){
		$post['yue_buynum']=intval($post['yue_buynum']);
		if(!$post['yue_buynum']){
			exi('请输入每天抢购次数,必须大于等于1！','error');
		}
		$post['yue_days']=intval($post['yue_days']);
		if(!$post['yue_days']){
			exi('请输入抢购天数,必须大于等于1！','error');
		}
	}
	$team=pdo_get('jy_robshop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goodsid));
	if(!$team['goodstype']){
		if(!$post['address'] || !$post['address']['name'] || !$post['address']['mobile'] || !$post['address']['address']){
			exi('请完善配送地址相关信息！','error');
		}
	}
	if($team['is_vipbuy'] && $team['vipprice']>0){
		$team['isvip']=$this->model('team')->check_vipbuy(0,$team);
	}
	$team['price']=$team['isvip']?$team['vipprice']:$team['price'];
	$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
	$this->model('model')->check_buy($team,$post,true,$member);
	if($_RQ['isyue']){
		$yue_fee=$this->setting['yue_fee']?:0;
		$yue_quan=$this->setting['yue_quan']?:0;
		$allnum=$post['yue_buynum']*$post['yue_days'];
		if($yue_fee){
			$yue_fee=$yue_fee*$allnum;
		}
		if($yue_quan){
			$yue_quan=$yue_quan*$allnum;
		}
		$goodsfee=$team['price']*$allnum;
		$allfee=$goodsfee+$yue_fee;
		$day=date('Y/m/d');
		if($team['jian_openday']){
			$starttime=strtotime($day)+24*3600;
		}else{
			$hourkey=$this->model('model')->gethourkey();
			if(!$hourkey){
				$starttime=strtotime($day)+12*3600;
			}elseif($hourkey==1){
				$starttime=strtotime($day)+18*3600;
			}elseif($hourkey==2){
				$starttime=strtotime($day)+24*3600;
			}
		}
		$log=array(
				'goodsid'=>$goodsid,
				'yue_fee'=>$yue_fee,
				'yue_quan'=>$yue_quan,
				'goodsfee'=>$goodsfee,
				'allfee'=>$allfee,
				'allnum'=>$allnum,
				'starttime'=>time(),
				'sendtype'=>$post['sendtype'],
				'address'=>ijson_encode($post['address']),
				'buydays'=>$post['yue_days'],
				'buynum'=>$post['yue_buynum'],
		);
		$order_id=$this->model('model')->up_yuelog($log);
		$params=array(
			'id'=>$order_id,
			'title'=>'预约抢购',
			'fee'=>$allfee,
			'back_url'=>'jy_robshop/detail/yuepayresult/',
			'success_url'=>'jy_robshop/yuelog/',
			'error_url'=>'jy_robshop/yuelog/',
		);
		$payid=cfc('pay')->create_pay_order($params);
		$this->pdo_update('yuelog',array('pay_id'=>$payid),array('id'=>$order_id));
		$res=array(
			'pay_id'=>$payid,
			'order_id'=>$order_id,
			'success_url'=>'jy_robshop/yuelog/',
			'error_url'=>'jy_robshop/yuelog/',
		);
		exi($res);
	}else{
		$log=array(
				'goodsid'=>$goodsid,
				'fee'=>$team['price'],
				'sendtype'=>$post['sendtype'],
				'address'=>ijson_encode($post['address']),
		);
		$order_id=$this->model('model')->up_canlog($log);
		$params=array(
			'id'=>$order_id,
			'title'=>'商品抢购',
			'fee'=>$team['price'],
			'back_url'=>'jy_robshop/detail/payresult/',
			'success_url'=>'jy_robshop/canlog/',
			'error_url'=>'jy_robshop/canlog/',
		);
		$payid=cfc('pay')->create_pay_order($params);
		$this->pdo_update('canlog',array('pay_id'=>$payid),array('id'=>$order_id));
		$res=array(
			'pay_id'=>$payid,
			'order_id'=>$order_id,
			'success_url'=>'jy_robshop/canlog/',
			'error_url'=>'jy_robshop/canlog/',
		);
		exi($res);
	}
}
if($_RQ['op']=='yuepayresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_robshop/yuelog/');
	}
	$order=pdo_get('jy_robshop_yuelog',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_robshop/yuelog/');
	}
	if($order['allfee']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['price'].'|'.$payresult['fee'],'error','jy_robshop/yuelog/');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
	);
	$this->model('model')->yuelog_pay($order,$params);
	exi('提交成功!');
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_robshop/canlog/');
	}
	$order=pdo_get('jy_robshop_canlog',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_robshop/canlog/');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['price'].'|'.$payresult['fee'],'error','jy_robshop/canlog/');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
	);
	$this->model('model')->canlog_pay($order,$params);
	exi('提交成功!');
}
?>